package com.juqimiao.raccoon.commodity.repository;

import com.juqimiao.raccoon.commodity.convertor.CategoryGroupConverter;
import com.juqimiao.raccoon.commodity.domain.category.Category;
import com.juqimiao.raccoon.commodity.domain.category.CategoryGroup;
import com.juqimiao.raccoon.commodity.domain.support.repository.CategoryGroupEntityRepI;
import com.juqimiao.raccoon.commodity.event.CategoryGroupPublishedEvent;
import com.juqimiao.raccoon.commodity.messaging.DomainEventServiceI;
import com.juqimiao.raccoon.commodity.tunnel.database.CategoryGroupTunnelI;
import com.juqimiao.raccoon.commodity.tunnel.database.dataobject.CategoryGroupDO;
import com.juqimiao.linn.exception.BusinessException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class CategoryGroupRepository implements CategoryGroupEntityRepI {
    private final CategoryGroupConverter converter;
    private final CategoryGroupTunnelI tunnel;
    private final DomainEventServiceI eventService;

    @Autowired
    public CategoryGroupRepository(CategoryGroupConverter converter,
                                   CategoryGroupTunnelI tunnel,
                                   DomainEventServiceI eventService) {
        this.converter = converter;
        this.tunnel = tunnel;
        this.eventService = eventService;
    }

    public String insert(CategoryGroup categoryGroup) {
        CategoryGroupDO categoryGroupDO = converter.toDataObject(categoryGroup);
        int result = tunnel.insert(categoryGroupDO);
        if (result <= 0) {
            throw new BusinessException("error");
        }

        return "success";
    }

    public int publish(List<CategoryGroup> categoryGroups) {
        int count = batchUpdate(categoryGroups);
        if (count > 0) {
            CategoryGroupPublishedEvent event = new CategoryGroupPublishedEvent();
            //execute in asyn.
            eventService.publish(event);
        }
        return count;
    }

    public int update(CategoryGroup categoryGroup) {
        List<CategoryGroupDO> groupDOS = new ArrayList<>();
        CategoryGroupDO categoryGroupDO = converter.toDataObject(categoryGroup);
        groupDOS.add(categoryGroupDO);
        return tunnel.batchUpdate(groupDOS);
    }

    public int batchUpdateSortOrder(List<CategoryGroup> categoryGroups) {
        List<CategoryGroupDO> categoryGroupDOList = new ArrayList<>();
        for (CategoryGroup group : categoryGroups) {
            categoryGroupDOList.add(converter.toDataObject(group));
        }

        return tunnel.batchUpdateSortOrder(categoryGroupDOList);
    }

    public int batchUpdate(List<CategoryGroup> categoryGroups) {

        List<CategoryGroupDO> categoryGroupDoList = new ArrayList<>();
        for (CategoryGroup group : categoryGroups) {
            categoryGroupDoList.add(converter.toDataObject(group));
        }
        return tunnel.batchUpdate(categoryGroupDoList);
    }

    @Override
    public CategoryGroup getById(String id) {
        CategoryGroupDO categoryGroupDo = tunnel.getById(id);
        return converter.toEntity(categoryGroupDo);
    }

    @Override
    public CategoryGroup getByName(String name, String groupType, String scenario) {
        CategoryGroupDO groupDO = tunnel.getByName(name, groupType, scenario);
        return converter.toEntity(groupDO);
    }

    @Override
    public List<CategoryGroup> getMany(String[] ids) {
        List<CategoryGroupDO> groupDOList = tunnel.getMany(ids);
        List<CategoryGroup> groups = new ArrayList<>();
        if (groupDOList != null
                && groupDOList.size() > 0) {
            groupDOList.forEach(group -> {
                groups.add(converter.toEntity(group));
            });
        }
        return groups;
    }
}
